/*
 * TestFul - http://code.google.com/p/testful/
 * Copyright (C) 2012 Matteo Miraz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package testful.evolutionary;

import java.util.Iterator;
import java.util.Map;

import jmetal.base.Variable;
import jmetal.util.JMException;
import jmetal.util.PseudoRandom;
import testful.model.Operation;
import ec.util.MersenneTwisterFast;

/**
 * TODO describe me!
 * 
 * @author Administrator
 */
public class PSO {

	/**
	 * compute the speed of swarm
	 * @throws JMException
	 */
	private <V extends Variable> void computeSpeed() throws JMException {
		double r1, r2, W, C1, C2;
		// DecisionVariables<Real> bestGlobal;
		MersenneTwisterFast random = PseudoRandom.getMersenneTwisterFast();

		PSOSolution<V> individual = null;
		PSOSolution<V> bestGlobal = null;
		PSOSolution<V> bestIndividual = null;
		float gPro = 0.2f;
		float iPro = 0.8f;

		SwapOperation gDiff = SwapUtil.getSwapOperation(bestGlobal, individual);
		SwapOperation iDiff = SwapUtil.getSwapOperation(bestIndividual,
				individual);

		Iterator gIter = gDiff.getSwapOperators().entrySet().iterator();
		while (gIter.hasNext()) {
			Map.Entry entry = (Map.Entry) gIter.next();
			String key = (String) entry.getKey();
			Operation op = (Operation) entry.getValue();

			if (random.nextBoolean(gPro)) {
				individual.getSwapOp().getSwapOperators().put(key, op);
			}
		}

		Iterator iIter = iDiff.getSwapOperators().entrySet().iterator();
		while (iIter.hasNext()) {
			Map.Entry entry = (Map.Entry) iIter.next();
			String key = (String) entry.getKey();
			Operation op = (Operation) entry.getValue();

			if (random.nextBoolean(iPro)) {
				individual.getSwapOp().getSwapOperators().put(key, op);
			}
		}
	}


	/**
	 * compute the new position of swarm
	 * @throws JMException
	 */
	private <V extends Variable> void computeNewPositions() throws JMException {

		PSOSolution<V> individual = null;

		SwapUtil.applyChange(individual, individual.getSwapOp());

	}

}
